import time
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job

from apps.tools.common_tools import *

try:
    # 实例化调度器
    scheduler = BackgroundScheduler()
    # 调度器使用DjangoJobStore()
    scheduler.add_jobstore(DjangoJobStore(), "default")


    # 'cron'方式循环，周一到周五，每天9:30:10执行,id为工作ID作为标记
    # ('scheduler',"interval", seconds=1) #用interval方式循环，每一秒执行一次
    @register_job(scheduler, 'cron', day_of_week='mon-fri', hour='12', minute='30', second='10', id='task_time')
    # @register_job(scheduler, 'interval', id='test', hours=0, minutes=1)
    def test_job():
        t_now = time.localtime()
        print(t_now)


    @register_job(scheduler, 'interval', id='test', hours=0, minutes=0, seconds=1)
    def test_two():

        # get_scrapyd_cli().schedule('yu_qing', 'movie')
        t_now = time.localtime()
        print(t_now)


    @register_job(scheduler, 'date', id='tt')  # 只执行一次的任务 这里可以写rabbitmq的消费任务
    def test_rabbitmq():
        t_now = time.localtime()
        print('--------77777')
        print(t_now)


    # 监控任务
    register_events(scheduler)
    # 调度器开始
    scheduler.start()
except Exception as e:
    print(e)
    # 报错则调度器停止执行
    scheduler.shutdown()
